﻿using Adnc.Application.Shared;
using Adnc.Application.Shared.Dtos;
using Adnc.WebApi.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WYT.Pro.Application.Contracts.Dtos.Product;
using WYT.Pro.Application.Contracts.Services;

namespace WYT.Pro.WebApi.Controllers
{
    /// <summary>
    /// 商品管理
    /// </summary>
    [Route("pro/[Controller]")]
    [ApiController]
    public class ProductController : AdncControllerBase
    {
        private readonly IProductAppService _productAppService;

        private readonly IUserContext _userContext;


        public ProductController(IProductAppService productAppService
            ,IUserContext userContext)
        {
            _productAppService = productAppService;
            _userContext = userContext;
        }

        /// <summary>
        /// 获取商品详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<WYTActionResult<ProductDetailDto>> GetProductDetailById([FromQuery] ProductSearchDto input)
        {
            return Success(await _productAppService.GetProductDetailById(input));
        }

        /// <summary>
        /// 获取云仓商品列表
        /// </summary>
        /// <param name="Language"></param>
        /// <returns></returns>
        [HttpGet("warList")]
        public async Task<WYTActionResult<List<ProductListDto>>> GetWarProductList([FromQuery] string Language)
        {
            return Success(await _productAppService.GetWarProductList(Language));
        }

        /// <summary>
        /// 查询商品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("serchByName")]
        public async Task<WYTActionResult<List<ProductListDto>>> ProductSearch([FromQuery] SimpleSearchDto<string> input)
        {
            return Success(await _productAppService.ProductSearch(input));
        }

        /// <summary>
        /// 获取周边产品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("perPro")]
        public async Task<WYTActionResult<List<ProductListDto>>> GetPeripheralProducts([FromQuery]SearchDto input)
        {
            return Success(await _productAppService.GetPeripheralProducts(input));
        }

        /// <summary>
        /// 获取通排调补信息
        /// </summary>
        /// <param name="language"></param>
        /// <returns></returns>
        [HttpGet("groupedGift")]
        public async Task<WYTActionResult<List<GroupedProductDto>>> GetGroupedProductInfor([FromQuery] string language)
        {
            return Success(await _productAppService.GetGroupedProductInfor(language));
        }

        /// <summary>
        /// 获取商品评论列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("reviewList")]
        public async Task<WYTActionResult<PageModelDto<ProductReviewListDto>>> GetProductReview([FromQuery]ProductReviewPagedSearcheDto input)
        {
            return Success(await _productAppService.GetProductReview(input));
        }

        /// <summary>
        /// 获取商品评论数量
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("reviewNum")]
        public async Task<WYTActionResult<ProductReviewQuantityDto>> GetProductReviewNum([FromQuery]SimpleDto<long> input)
        {
            return Success(await _productAppService.GetProductReviewNum(input.Value));
        }

        /// <summary>
        /// 评论回复
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("reviewReply")]
        public async Task<WYTActionResult> ReviewReply(ProductReviewReplyAddDto input)
        {
            return Success(await _productAppService.ReviewReply(_userContext.Id, input));
        }

        /// <summary>
        /// 获取评论详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("reviewDetail")]
        public async Task<WYTActionResult<ProductReviewDetailDto>> GetReviewDetail([FromQuery] SimpleDto<long> input)
        {
            return Success(await _productAppService.GetReviewDetail(input.Value));
        }

        #region 内部接口

        /// <summary>
        /// 获取规格ID获取商品详情
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="hasGift">是否返回商品的赠品信息</param>
        /// <returns></returns>
        [HttpGet("_sizeDetail")]
        [AllowAnonymous]
        public async Task<ActionResult<List<ProductSizeWithGiftDetailDto>>> GetProductSizeDetailById([FromQuery] long[] ids, [FromQuery] bool hasGift = false)
        {
            return Result(await _productAppService.GetProductSizeDetailById(ids,hasGift));
        }

        /// <summary>
        /// 获取商品简单信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet("_sizeSimple")]
        [AllowAnonymous]
        public async Task<ActionResult<List<ProductSizeSimpleInforDto>>> GetProductSizeSimpleInforById([FromQuery] long[] ids)
        {
            return Result(await _productAppService.GetProductSizeSimpleInforById(ids));
        }

        /// <summary>
        /// 获取所有云仓商品详情
        /// </summary>
        /// <returns></returns>
        [HttpGet("_AllWar")]
        [AllowAnonymous]
        public async Task<ActionResult<List<ProductSizeSimpleInforDto>>> GetProductOriginalPrice()
        {
            return Result(await _productAppService.GetAllWarehouseProduct());
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("_addReview")]
        public async Task<ActionResult<bool>> ReviewAdd(ProductReviewAddDto input)
        {
            return Result(await _productAppService.ReviewAdd(input));
        }
        #endregion
    }
}
